package com.frogs.ucenter.service.impl;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.PageTurn;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.BrowsingService;

@Service("browsingService")
public class BrowsingServiceImpl extends GoodsBaseServiceImpl implements BrowsingService {
	
	private static Logger log = LoggerFactory.getLogger(BrowsingServiceImpl.class);
	
	/**
	 * 浏览记录列表，待增加用户行为逻辑
	 */
	@Override
	public JSONObject getUserBrowsingList(Integer pageNo, Integer pageSize,
			Integer userId) {
		return null;
	}
	@Override
	public PageList<Map<String,Object>> getUserHistoryList(Integer pageNo, Integer pageSize,
			String userId) throws Exception {
		ApiUrl apiUrl = this.buildTraceApiBaseUrl("TRACE_USER_HISTORY");
		String[] otherParams = {ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString(),
								ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
								ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		PageList<Map<String,Object>> result = new PageList<Map<String,Object>>();
		JSONObject jsonObjectResult = null;
		try{
			jsonObjectResult = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != jsonObjectResult.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(jsonObjectResult.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			JSONArray listJSONArray = jsonObjectResult.getJSONObject("response").getJSONArray("list");
	    	Map<Integer, JSONObject> goodsMap = super.getGoodsMap(listJSONArray);
	    	result.addAll(super.mergeGoods(listJSONArray, goodsMap));
	    	JSONObject pageTurnJSONObject = jsonObjectResult.getJSONObject("response").getJSONObject("pageTurn");
	    	result.setPageTurn(JSON.toJavaObject(pageTurnJSONObject, PageTurn.class));
		} catch (IllegalStateException e) {
			log.error("func[getUserHistoryList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("用户浏览接口异常！");
		}
		return result;
	}
	
	/**
	 * 删除浏览记录
	 */
	@Override
	public JSONObject deleteUserBrowsing(Integer pageNo, Integer pageSize,
			Integer userId, Integer browsingId) throws Exception {
		ApiUrl apiUrl = this.buildTraceApiBaseUrl("USER_HISTORY_DELETE");
		String[] otherParams = {ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString(),
								ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
								ApiUrlParamEnum.userId.getParamName(),userId.toString(),
								ApiUrlParamEnum.browsingId.getParamName(), browsingId.toString()
								};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject jsonObjectResult = null;
		try{
			jsonObjectResult = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != jsonObjectResult.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(jsonObjectResult.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return jsonObjectResult;
		} catch (IllegalStateException e) {
			log.error("func[deleteUserBrowsing] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}catch(Exception e){
			throw new Exception("删除用户浏览接口异常！");
		}
		
		return null;
	}

}
